package com.yanchangapp.base;

import android.util.Log;

import com.google.gson.Gson;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.model.bean.DailyPlanBean;
import com.yanchangapp.model.bean.GoodsApplyBean;
import com.yanchangapp.model.bean.LookLinkWorkBean;
import com.yanchangapp.model.bean.NormalPageBean;
import com.yanchangapp.model.bean.OrderDetailBean;
import com.yanchangapp.model.bean.POrderCntListBean;
import com.yanchangapp.model.bean.PassApproveBean;
import com.yanchangapp.model.bean.ProductExtractBean;
import com.yanchangapp.model.bean.RecordEmailBean;
import com.yanchangapp.model.bean.Version;
import com.yanchangapp.model.bean.WareHouseBean;
import com.yanchangapp.model.bean.WorkItemBean;
import com.yanchangapp.model.cache.UserCache;
import com.yanchangapp.model.request.ApproveListRequest;
import com.yanchangapp.model.request.CntListRequest;
import com.yanchangapp.model.request.ContractListRequest;
import com.yanchangapp.model.request.CreateContractRequest;
import com.yanchangapp.model.request.CreateOrderRequest;
import com.yanchangapp.model.request.DailyPlansRequest;
import com.yanchangapp.model.request.DelayPlanRequest;
import com.yanchangapp.model.request.EditContractRequest;
import com.yanchangapp.model.request.EditOrderRequest;
import com.yanchangapp.model.request.EmptyRequest;
import com.yanchangapp.model.request.FinishPlanRequest;
import com.yanchangapp.model.request.GoodsApplyRequest;
import com.yanchangapp.model.request.MailListrequest;
import com.yanchangapp.model.request.MakeDailyPlanRequest;
import com.yanchangapp.model.request.MakeDailyPlanRequestPlus;
import com.yanchangapp.model.request.MakePlanManRequest;
import com.yanchangapp.model.request.MakePlanWorkRequest;
import com.yanchangapp.model.request.MsgCntRequest;
import com.yanchangapp.model.request.PlanHistoryRequest;
import com.yanchangapp.model.request.PsListRequest;
import com.yanchangapp.model.request.PutRecordRequest;
import com.yanchangapp.model.request.QueryLinkManRequest;
import com.yanchangapp.model.request.RecordDetailRequest;
import com.yanchangapp.model.request.RecordListRequest;
import com.yanchangapp.model.request.RejectRecordRequest;
import com.yanchangapp.model.request.SaveDraftRequest;
import com.yanchangapp.model.request.SendMailRequest;
import com.yanchangapp.model.request.SendTotalMailRequest;
import com.yanchangapp.model.request.SubmitApplyRequest;
import com.yanchangapp.model.request.UpRecordRequest;
import com.yanchangapp.model.request.UpdateIDsRequest;
import com.yanchangapp.model.request.UpdatePhoneRequest;
import com.yanchangapp.model.request.UpdatePwdRequest;
import com.yanchangapp.model.request.WorksRequest;
import com.yanchangapp.model.response.AllLinkManResponse;
import com.yanchangapp.model.response.ClauseEditResponse;
import com.yanchangapp.model.response.CntListResponse;
import com.yanchangapp.model.response.ContractDetailResponse;
import com.yanchangapp.model.response.ContractListResponse;
import com.yanchangapp.model.response.CreateContractResponse;
import com.yanchangapp.model.response.CreateOrderResponse;
import com.yanchangapp.model.response.EditContractResponse;
import com.yanchangapp.model.response.EditOrderResponse;
import com.yanchangapp.model.response.GoodsApplyResponse;
import com.yanchangapp.model.response.GoodsApproveResponse;
import com.yanchangapp.model.response.HomeBannerResponse;
import com.yanchangapp.model.response.MailDetailResponse;
import com.yanchangapp.model.response.MailListResponse;
import com.yanchangapp.model.response.MailUnreadResponse;
import com.yanchangapp.model.response.MenuResponse;
import com.yanchangapp.model.response.MgDailyListResponse;
import com.yanchangapp.model.response.MsgContentResponse;
import com.yanchangapp.model.response.OperateBookResponse;
import com.yanchangapp.model.response.PlanDetailResponse;
import com.yanchangapp.model.response.PsListResponse;
import com.yanchangapp.model.response.RecordDetailResponse;
import com.yanchangapp.model.response.RecordListResponse;
import com.yanchangapp.model.response.RelatedManResponse;
import com.yanchangapp.model.response.SupplierListResponse;
import com.yanchangapp.model.response.UpdateRecordResponse;
import com.yanchangapp.model.response.WaitDoneResponse;
import com.yanchangapp.model.response.WorkItemDetailResponse;
import com.zxl.baselib.bean.response.BaseListResponse;
import com.zxl.baselib.bean.response.BasePageListResponse;
import com.zxl.baselib.bean.response.BaseResponse;

import java.util.List;

import io.reactivex.Observable;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 带有头部的retrofit
 */
public class HeadApiRetrofit extends WithHeadClient{

    public MyApi mApi;

    private HeadApiRetrofit(){
        //在构造方法中完成对Retrofit接口的初始化
        mApi = new Retrofit.Builder()
                .baseUrl(MyApi.BASE_URL)
                .client(getClient())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build()
                .create(MyApi.class);
    }

    private static class  ApiRetrofitHolder{
        private static final HeadApiRetrofit M_INSTANCE =  new HeadApiRetrofit();
    }

    private static HeadApiRetrofit mHeadApiRetrofit = null;

    /**
     * 启示 有时候赞么需要单例 但也需要学会重置
     * @return
     */
    public static HeadApiRetrofit getInstance(){
        if (mHeadApiRetrofit == null){
            synchronized (HeadApiRetrofit.class){
                if (mHeadApiRetrofit == null){
                    mHeadApiRetrofit = new HeadApiRetrofit();
                }
            }
        }
        return mHeadApiRetrofit;
    }

    public static void resetHead(){
        Log.e("全局", "resetHead: "+"===============");
        mHeadApiRetrofit = null;
    }


    /**
     * 修改密码
     * @return
     */
    //
    public Observable<BaseResponse<Version>> checkVersionUpdate(){
        return mApi.checkUpdate();
    }

    public Observable<BaseResponse> updatePwd(int id, String pwd){
        return  mApi.updatePassword(getRequestBody(new UpdatePwdRequest(id, pwd)));
    }


    public Observable<BaseResponse> updateMobile(int id, String mobile, String sms){
        return mApi.updateMobile(getRequestBody(new UpdatePhoneRequest(id,mobile,sms)));
    }

    public Observable<HomeBannerResponse> queryBanner(){
        return mApi.queryBanner();
    }

    public Observable<WaitDoneResponse> queryWaitDoneList(){
        return mApi.queryWaitDoneList();
    }

    public Observable<MenuResponse> queryMenuLists(){
        return mApi.queryMenuLists(UserCache.getEmployeeId());
    }


    public Observable<MsgContentResponse> queryMessageList(Integer keywords,int currPage){
        return mApi.queryMessageList(getRequestBody(new MsgCntRequest(keywords,currPage, AppConstant.ONE_PAGE_SIZE)));
    }


    public Observable<BaseResponse> setReadedMessageList(List<Integer> mMsgList){
        return mApi.setReadedMessageList(getRequestBody(new UpdateIDsRequest(mMsgList)));
    }


    public  Observable<MailListResponse> queryMailListInfo(String netMailType,int currentPage,String keywords){
        return  mApi.queryMailListInfo(netMailType,getRequestBody(new MailListrequest(currentPage,keywords)));
    }


    public Observable<MailDetailResponse> queryMailDetailInfo(int mailID){
        return mApi.queryMailDetailInfo(mailID);
    }

    public Observable<BaseResponse> setMailSeenState(int seenType,List<Integer> mSelectLists){
        return mApi.setMailSeenState(seenType,mSelectLists);
    }

    public Observable<BaseResponse> moveMailsToTargetMails(String netMailType,List<Integer> mSelectLists){
        return mApi.moveMailsToTargetMails(netMailType,mSelectLists);
    }


    public Observable<MailUnreadResponse> getUnReadCount(){
        return mApi.getUnReadCount();
    }

    public Observable<BaseResponse> actualDeleteMail(List<Integer> mSelectLists){
        return  mApi.actualDeleteMail(mSelectLists);
    }

    // 单单是发送邮件....
    public Observable<BaseResponse> sendMail(SendMailRequest sendMailRequest){
        return mApi.sendMail(getRequestBody(sendMailRequest));
    }

    public Observable<BaseResponse> sendTotalMail(SendTotalMailRequest sendTotalMailRequest){
        return mApi.sendTotalMail(getRequestBody(sendTotalMailRequest));
    }

    public Observable<BaseResponse> saveMailToDraft(SaveDraftRequest saveDraftRequest){
        return mApi.saveMailToDraft(getRequestBody(saveDraftRequest));

    }

    public Observable<CntListResponse> queryMailContacts(){
        return mApi.queryMailContacts(UserCache.getCompanyId());
    }

    public Observable<MgDailyListResponse> queryDailyPlanLists(){
        return mApi.queryDailyPlanLists(UserCache.getEmployeeId(),getRequestBody(new EmptyRequest()));
    }

    public Observable<BaseResponse> startWorkPlan(int planId){
        return mApi.startWorkPlan(planId);
    }

    public Observable<BaseResponse> cancelWorkPlan(int planId){
        return  mApi.cancelWorkPlan(planId);
    }

    public Observable<BaseResponse> delayWorkPlan(int planId, DelayPlanRequest delayPlanRequest){
        return mApi.delayWorkPlan(planId,getRequestBody(delayPlanRequest));
    }


    public Observable<PsListResponse> queryPurchaseList(PsListRequest psListRequest){
        return mApi.queryPurchaseList(UserCache.getCompanyId(),getRequestBody(psListRequest));
    }

    public Observable<BaseListResponse<POrderCntListBean>> queryCmyContractList(String keywords){
        return  mApi.queryCmyContractList(UserCache.getCompanyId(),getRequestBody(new CntListRequest(keywords)));
    }

    public Observable<BaseListResponse<ProductExtractBean>> queryProductExtractList(String contractId){
        return  mApi.queryProductExtractList(contractId,getRequestBody(new EmptyRequest()));
    }

    public Observable<BaseListResponse<ProductExtractBean>> querySupplierProductList(String supplierID){
        return mApi.querySupplierProductList(supplierID,getRequestBody(new EmptyRequest()));
    }

    public Observable<com.zxl.baselib.bean.response.BaseResponse<OrderDetailBean>> getPurchaseOrderDetail(String orderId){
        return mApi.getPurchaseOrderDetail(orderId);
    }

    public Observable<BaseResponse> deleteOrderById(String orderId){
        return mApi.deleteOrderById(orderId);
    }

    public Observable<EditOrderResponse> putEditOrder(EditOrderRequest editOrderRequest){
        return mApi.putEditOrder(getRequestBody(editOrderRequest));
    }

    public Observable<BaseResponse> submitApprovalOrderByID(String orderId){
        return mApi.submitApprovalOrderByID(orderId);
    }


    public Observable<CreateOrderResponse> createOrder(CreateOrderRequest createOrderRequest){
        return mApi.createOrder(getRequestBody(createOrderRequest));
    }

    public Observable<BaseListResponse<WareHouseBean>> queryAllWareHouse(){
        return mApi.queryAllWareHouse();
    }

    public Observable<SupplierListResponse> querySupperLists(){
        return mApi.querySupperLists(UserCache.getCompanyId(),getRequestBody(new NormalPageBean("",0,0)));
    }

    public Observable<ContractListResponse> queryContractsList(ContractListRequest contractListRequest){
        return mApi.queryContractsList(UserCache.getCompanyId(),getRequestBody(contractListRequest));
    }

    // 获得合同的详情...
    public Observable<BaseResponse<ContractDetailResponse>> getContractDetail(String id){
        return mApi.getContractDetail(id);
    }

    public Observable<ClauseEditResponse> queryDefaultClause(){
        return mApi.queryDefaultClause();
    }

    public Observable<CreateContractResponse> createContract(CreateContractRequest request){
        return mApi.createContract(getRequestBody(request));
    }

    public Observable<EditContractResponse> editContract(EditContractRequest request){
        return mApi.editContract(getRequestBody(request));
    }

    public Observable<BaseResponse> submitApprovalContractByID(String contractId){
        return mApi.submitApprovalContractByID(contractId);
    }

    public Observable<GoodsApplyResponse> queryApplyGoddsList(int curPage){
        return mApi.queryApplyGoodsList(UserCache.getCompanyId(),getRequestBody(new GoodsApplyRequest(curPage)));
    }

    public Observable<BaseResponse<GoodsApplyBean>> queryApplyDetail(String id,int isThirdFlag){
        return mApi.queryApplyDetail(id,isThirdFlag);
    }

    public Observable<BaseResponse> submitGoodsApply(List<SubmitApplyRequest> submitApplyRequests){
        return mApi.submitGoodsApply(submitApplyRequests);
    }


    public Observable<RecordListResponse> queryRecordList(RecordListRequest recordListRequest){
        return mApi.queryRecordList(getRequestBody(recordListRequest));
    }

    // 查询记录详情...
    public Observable<BaseResponse<RecordDetailResponse>> queryRecordDetailByItemId(int id){
        return mApi.queryRecordDetail(id,getRequestBody(new RecordDetailRequest(0,0)));
    }

    // 查询审批列表
    public Observable<GoodsApproveResponse> queryApproveList(ApproveListRequest approveListRequest){
        return mApi.queryApproveList(getRequestBody(approveListRequest));
    }

    public Observable<BaseResponse<PassApproveBean>> queryAvDetailByOrderId(String orderId){
        return mApi.queryAvDetailByOrderId(orderId);
    }

    public Observable<UpdateRecordResponse> updateRecordProduct(UpRecordRequest request){
        return mApi.updateRecordProduct(getRequestBody(request));
    }

    public Observable<BaseResponse> deleteRecordByID(int id){
        return mApi.deleteRecordByID(id);
    }

    public Observable<BaseResponse> submitRecordByID(int id){
        return mApi.submitRecordByID(id,getRequestBody(new PutRecordRequest(0)));
    }

    public Observable<BaseResponse> rejectRecord(RejectRecordRequest request){
        return mApi.rejectRecord(getRequestBody(request));
    }

    public Observable<BaseResponse> passRecord(int id){
        return mApi.passRecord(id);
    }

    public Observable<BaseListResponse<RecordEmailBean>> queryMailContent(int id){
        return mApi.queryMailContent(id);
    }

    public Observable<BaseResponse<OperateBookResponse>> getOperateDetail(long id){
        return mApi.getOperateDetail(id);
    }

    public Observable<BaseResponse> putStartPlan(long id){
        return mApi.putStartPlan(id);
    }

    public Observable<BaseResponse> putFinishPlan(long id ,FinishPlanRequest finishPlanRequest){
        return mApi.putFinishPlan(id,getRequestBody(finishPlanRequest));
    }

    public Observable<BaseResponse> putCannotFinishPlan(long id,FinishPlanRequest finishPlanRequest){
        return mApi.putCannotFinishPlan(id,getRequestBody(finishPlanRequest));
    }

    public Observable<BaseResponse> putUpdateFinishPlan(long id,FinishPlanRequest finishPlanRequest){
        return mApi.putUpdateFinishPlan(id,getRequestBody(finishPlanRequest));
    }

    public Observable<BasePageListResponse<WorkItemBean>> postQueryWorkItemList(WorksRequest worksRequest){
        return mApi.postQueryWorkItemList(getRequestBody(worksRequest));
    }

    public Observable<BaseResponse<WorkItemDetailResponse>> getQueryWorkItemDetail(long id){
        return mApi.getQueryWorkItemDetail(id);
    }

    public Observable<BaseResponse> postMakeDailyPlan(List<MakeDailyPlanRequest> list){
        return mApi.postMakeDailyPlan(getRequestBody(new MakeDailyPlanRequestPlus(list)));
    }

    // 没错
    public Observable<BaseResponse> postMakePlanFromMan(MakePlanManRequest request){
        return mApi.postMakePlanFromMan(getRequestBody(request));
    }

    public Observable<BaseResponse> postMakePlanFromWork(MakePlanWorkRequest makePlanWorkRequest){
        return mApi.postMakePlanFromWork(getRequestBody(makePlanWorkRequest));
    }

    public Observable<BaseResponse<PlanDetailResponse>> getQueryPlanDetail(long planID){
        return mApi.getQueryPlanDetail(planID);
    }

    public Observable<BasePageListResponse<DailyPlanBean>> postQueryHistoryPlans(PlanHistoryRequest planHistoryRequest){
        return mApi.postQueryHistoryPlans(getRequestBody(planHistoryRequest));
    }

    public Observable<BasePageListResponse<DailyPlanBean>> postQueryDailyPlans(DailyPlansRequest dailyPlansRequest){
        return mApi.postQueryDailyPlans(getRequestBody(dailyPlansRequest));
    }

    public Observable<BaseListResponse<RelatedManResponse>> getQueryLookMan(long id){
        return mApi.getQueryLookMan(id);
    }

    public Observable<BaseListResponse<LookLinkWorkBean>> getQueryLookWorkItem(long id){
        return mApi.getQueryLookWorkItem(id,getRequestBody(new DailyPlansRequest(0,0)));
    }

    public Observable<BaseListResponse<AllLinkManResponse>> getQueryAllLinkMan(QueryLinkManRequest queryLinkManRequest){
        return mApi.getQueryAllLinkMan(getRequestBody(queryLinkManRequest));
    }

    /**
     * bean 转body
     * @param obj
     * @return
     */
    private RequestBody getRequestBody(Object obj) {
        String route = new Gson().toJson(obj);
        return RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),route);
    }
}
